package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampCustBizExtInfo;
import com.telecom.cms.util.Constant;

public class CampCustBizExtInfoDAO {

	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampCustBizExtInfo transientInstance) {
		int rs = 0;
		String sql = "select max(biz_info_id) from  " + db
				+ ".Camp_Cust_Biz_Ext_Info where Latn_Id='"
				+ transientInstance.getLatnId() + "' and cust_id='"
				+ transientInstance.getCustId() + "' and prd_inst_id='"
				+ transientInstance.getPrdInstId() + "'";
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "insert into " + db + ".Camp_Cust_Biz_Ext_Info values('"
						+ transientInstance.getLatnId() + "','"
						+ transientInstance.getCustId() + "','"
						+ transientInstance.getPrdInstId() + "',"
						+ rs2.getInt(1) + "+1,'"
						+ transientInstance.getBizInfo() + "','"
						+ transientInstance.getCollector() + "',"
						+ transientInstance.getCollecttime() + ")";
			else
				sql = "insert into " + db + ".Camp_Cust_Biz_Ext_Info values('"
						+ transientInstance.getLatnId() + "','"
						+ transientInstance.getCustId() + "','"
						+ transientInstance.getPrdInstId() + "'," + "1,'"
						+ transientInstance.getBizInfo() + "','"
						+ transientInstance.getCollector() + "',"
						+ transientInstance.getCollecttime() + ")";
			rs = dao.insert(sql);
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampCustBizExtInfo persistentInstance) {

		String sql = "delete from " + db
				+ ".Camp_Cust_Biz_Ext_Info where Cust_Id = '"
				+ persistentInstance.getCustId()+"' and prd_inst_id='"
				+persistentInstance.getPrdInstId()+"' and Biz_Info_Id='"
				+persistentInstance.getBizInfoId()+"'";

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampCustBizExtInfo findById(String custId, String prdInstId,
			String bizInfoId) {

		String sql = "select * from " + db
				+ ".Camp_Cust_Biz_Ext_Info where Cust_Id = '" + custId
				+ "' and prd_Inst_Id = '" + prdInstId + "' and Biz_Info_Id ='" + bizInfoId+"'";
		CampCustBizExtInfo sd = new CampCustBizExtInfo();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setLatnId(rs.getString(1));
				sd.setCustId(rs.getString(2));
				sd.setPrdInstId(rs.getString(3));
				sd.setBizInfoId(rs.getInt(4));
				sd.setBizInfo(rs.getString(5));
				sd.setCollector(rs.getString(6));
				sd.setCollecttime(rs.getString(7));
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll(String Cust_Id, String prd_inst_id) {

		String sql = "select * from " + db
				+ ".Camp_Cust_Biz_Ext_Info where Cust_Id='" + Cust_Id
				+ "' and prd_inst_id='" + prd_inst_id
				+ "' order by Biz_Info_Id";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampCustBizExtInfo sd = new CampCustBizExtInfo();
				sd.setLatnId(rs.getString(1));
				sd.setCustId(rs.getString(2));
				sd.setPrdInstId(rs.getString(3));
				sd.setBizInfoId(rs.getInt(4));
				sd.setBizInfo(rs.getString(5));
				sd.setCollector(rs.getString(6));
				sd.setCollecttime(rs.getString(7));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
